package ro.imc.rest.client.display;

import java.util.List;

import ro.imc.rest.client.display.components.CatalogsFrom;
import ro.imc.rest.client.display.components.CoursesFrom;
import ro.imc.rest.client.display.menu.MenuItemObject;
import ro.imc.rest.client.display.menu.MenuItemType;
import ro.imc.rest.client.display.util.DialogForm;
import ro.imc.rest.client.display.util.MessageConstants;
import ro.imc.rest.client.services.ServiceType;
import ro.imc.rest.client.services.forserver.RestService;
import ro.imc.rest.client.services.forserver.RestServiceAsync;
import ro.imc.rest.shared.dto.LearningObject;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.FlowPanel;


public class WindowForm extends DialogBox {
	
	public static ServiceType type;
	private static final Binder binder = GWT.create(Binder.class);
	@UiField Image closeButton;
	@UiField Image maximizeButton;
	@UiField Image minimizeButton;
	@UiField Label strTitle;
	@UiField AbsolutePanel content;
	@UiField Image strIcon;
	@UiField AbsolutePanel statusBar;
	private RestLogin user;
	interface Binder extends UiBinder<Widget, WindowForm> {
	}

	public WindowForm(MenuItemObject object, RestLogin user) {
		setWidget(binder.createAndBindUi(this));
		setModal(false);
		setAnimationEnabled(false);
		strTitle.setText(object.getTitle());
		strIcon.setUrl(object.getIcoUrl());
		this.user = user;
		loadContent(object.getType());
	}

	@UiHandler("closeButton")
	void onCloseButtonClick(ClickEvent event) {
		hide();
		clear();
	}
	@UiHandler("maximizeButton")
	void onMaximizeButtonClick(ClickEvent event) {
		//setPopupPosition(0, 70);
//		DOM.setStyleAttribute(statusBar.getElement(), "width", "100%");// (Window.getClientWidth()-20)+"px");
//		DOM.setStyleAttribute(content.getElement(), "width", "100%");
//		DOM.setStyleAttribute(content.getElement(), "height", (Window.getClientHeight()-90)+"px");
//		statusBar.setWidth("100%");
//	    content.setWidth("100%");
//	    content.setHeight("100%");
	}
	
	private void loadContent(MenuItemType type) {
		switch (type){
		case CATALOGS: {
			content.add(new CatalogsFrom());
		}
		case MY_COURSES: {
			//content.add(new Course)
		}
		
		case OPTIONS : {
			content.add(new OptionsForm(user));
		}
		}
	}
	
	private void loadPanelWithCourseData() {
		switch (OptionsForm.getCurrentType()) {
		case GWT_CLIENT :{
			
		}
		case GWT_SERVER: {
			
		}
		case GWT_JERSEY: {
			RestServiceAsync jerseyService = GWT.create(RestService.class);
			jerseyService.getCoursesList(user, new AsyncCallback<List<LearningObject>>() {
				
				@Override
				public void onSuccess(List<LearningObject> result) {
					content.add(new CoursesFrom(user, result));
				}
				
				@Override
				public void onFailure(Throwable caught) {
					DialogForm.displayDialog(MessageConstants.REST_ERR_TITLE, 
						caught.getMessage(),
						MessageConstants.REST_ERR_MESSAGE, 
						MessageConstants.REST_ERR_TIP);
				}
			});
		}
		
		}
		
	}
}
